package com.joven.shelltest.utils.pdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.mysql.cj.util.LRUCache;
/**
 * @Author wzj
 * @Date 2023-08-24 08:40
 **/
public abstract class PdfBase {
    /**
     * 默认标题字体大小
     */
    protected static final float CELL_PADDING = 6F;

    /**
     * 默认标题字体大小
     */
    protected static final int TITLE_FONT_SIZE = 18;

    /**
     * 默认小标题字体大小
     */
    protected static final int SMALL_TITLE_FONT_SIZE = 12;

    /**
     * 默认内容字体大小
     */
    protected static final int TEXT_FONT_SIZE = 9;

    protected static BaseFont BASE_FONT;
    static {
        try {
            BASE_FONT = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 缓存自定义字体,最多存放128个
     */
    protected static final LRUCache<String, Font> CACHE_MAP = new LRUCache<>(128);

    /**
     * 构造画笔类
     */
    public static class PdfFontBuilder {
        /**
         * 单元格 字体大小
         */
        private Integer fontSize = TEXT_FONT_SIZE;
        /**
         * 字体样式 0-Font.NORMAL, 1-Font.NORMAL, 2-Font.NORMAL, 3-Font.NORMAL, 4-Font.NORMAL, 5-Font.NORMAL, 6-Font.NORMAL, 7-Font.NORMAL, 8-Font.NORMAL, 9-Font.NORMAL
         */
        /**
         * 字体样式
         * <pre>
         *     Font.NORMAL       (0) 正常
         *     Font.BOLD     (1) 加粗
         *     Font.ITALIC      (2) 倾斜
         *     Font.UNDERLINE  (3) 下划线
         *     Font.STRIKETHRU  (4)
         *     Font.BOLDITALIC  (5) 加粗倾斜
         * </pre>
         */
        private Integer fontStyle = Font.NORMAL;
        /**
         * 字体颜色
         */
        private BaseColor fontColor = BaseColor.BLACK;

        /**
         * 构造类
         */
        public static PdfFontBuilder builder() {
            return new PdfFontBuilder();
        }

        /**
         * 构造函数，默认纸张A4
         */
        private PdfFontBuilder() {
        }

        @Override
        public String toString() {
            return "PdfFont[" + fontSize + "," + fontStyle + "," + fontColor + "]";
        }

        public PdfFontBuilder fontSize(int fontSize) {
            this.fontSize = fontSize;
            return this;
        }

        public PdfFontBuilder fontStyle(int fontStyle) {
            this.fontStyle = fontStyle;
            return this;
        }

        public PdfFontBuilder fontColor(BaseColor fontColor) {
            this.fontColor = fontColor;
            return this;
        }

        public Font build() {
            Font font;
            if (CACHE_MAP.containsKey(this.toString())) {
                font = CACHE_MAP.get(this.toString());
            } else {
                font = new Font(BASE_FONT, fontSize, fontStyle, fontColor);
                CACHE_MAP.put(this.toString(), font);
            }
            return font;
        }
    }
}
